package by.bsu.scs.presentation.form;

import java.io.File;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;
import org.apache.struts.util.LabelValueBean;

import by.bsu.scs.model.Message;
import by.bsu.scs.util.manager.SessionManager;

/**
 * 
 * @author Svetlana Voiteh
 * @version 1.0
 *
 * Message form bean.
 */

public class MessageForm extends ActionForm {
	
	public static final String SEND_MESSAGES = "send";
	public static final String RECEIVE_MESSAGES = "receive";

	private static final long serialVersionUID = 1L;
	private String message;
	private int disciplineId;
	private int userToId;
	private int courseId;
	private int groupId;
	private int countNotReadMessages;
	private ArrayList<LabelValueBean> disciplineList;
	private ArrayList<LabelValueBean> courseList;
	private ArrayList<LabelValueBean> groupList;
	private ArrayList<LabelValueBean> userList;
	private ArrayList<Message> messageList;
	private Message messageAnswer;
	private Message messageQuestion;
	private FormFile fileAttach;
	private File file;

	
	public int getCountNotReadMessages() {
		return countNotReadMessages;
	}

	public void setCountNotReadMessages(int countNotReadMessages) {
		this.countNotReadMessages = countNotReadMessages;
	}

	public FormFile getFileAttach() {
		return fileAttach;
	}

	public void setFileAttach(FormFile fileAttach) {
		this.fileAttach = fileAttach;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public Message getMessageQuestion() {
		return messageQuestion;
	}

	public void setMessageQuestion(Message messageQuestion) {
		this.messageQuestion = messageQuestion;
	}

	public Message getMessageAnswer() {
		return messageAnswer;
	}

	public void setMessageAnswer(Message messageAnswer) {
		this.messageAnswer = messageAnswer;
	}

	public ArrayList<Message> getMessageList() {
		return messageList;
	}

	public void setMessageList(ArrayList<Message> messageList) {
		this.messageList = messageList;
	}

	public int getUserToId() {
		return userToId;
	}

	public void setUserToId(int userToId) {
		this.userToId = userToId;
	}

	public int getCourseId() {
		return courseId;
	}

	public void setCourseId(int courseId) {
		this.courseId = courseId;
	}

	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	public ArrayList<LabelValueBean> getCourseList() {
		return courseList;
	}

	public void setCourseList(ArrayList<LabelValueBean> courseList) {
		this.courseList = courseList;
	}

	public ArrayList<LabelValueBean> getGroupList() {
		return groupList;
	}

	public void setGroupList(ArrayList<LabelValueBean> groupList) {
		this.groupList = groupList;
	}

	public ArrayList<LabelValueBean> getUserList() {
		return userList;
	}

	public void setUserList(ArrayList<LabelValueBean> userList) {
		this.userList = userList;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getDisciplineId() {
		return disciplineId;
	}

	public void setDisciplineId(int disciplineId) {
		this.disciplineId = disciplineId;
	}

	public ArrayList<LabelValueBean> getDisciplineList() {
		return disciplineList;
	}

	public void setDisciplineList(ArrayList<LabelValueBean> disciplineList) {
		this.disciplineList = disciplineList;
	}

	/**
	 * Проверяет, чтобы все поля нужные для отправки собщения были заполнены
	 * и чтобы они были заполнены корректно.
	 * 
	 * @param mapping
	 * @param request
	 * 
	 * @return errors
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		if ((this.userToId == 0) || (this.disciplineId == 0)) {
			if (SessionManager.getInstance().getUser(request).getUserRole()
					.getRoleName().equalsIgnoreCase("student")
					&& (this.userToId == 0)) {
				errors.add("userTo", new ActionMessage("error.userTo"));
			}
			if (SessionManager.getInstance().getUser(request).getUserRole()
					.getRoleName().equalsIgnoreCase("instructor")
					&& (this.disciplineId == 0) && (this.messageAnswer == null)) {
				errors.add("discipline", new ActionMessage("error.discipline"));
			}
		}

		if (this.message == null || this.message.equalsIgnoreCase("")) {
			errors.add("message", new ActionMessage("error.message"));
			this.disciplineId = 0;
		}
		

		if (this.fileAttach != null) {
			/*
			 * If the file size is greater than 20kb.
			 */
			if (this.fileAttach.getFileSize() > 1024 * 20) {
				errors.add("fileAttach", new ActionMessage(
						"error.fileAttach.size"));
			}
		}
		return errors;
	}

}
